﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinUI {
    public class PostgresqlJspGenerator {

        #region NEW
        public static string GenNewJSP(PostgreSqlTable table) {
            string displayName = table.DisplayName;
            string nameLowerCase = table.JavaNameLowerCase;

            string requiredInputs = GenNewRequiredInputs(table);
            string intInputs = GenNewIntInputs(table);
            string doubleInputs = GenNewDoubleInputs(table);
            string dateInputs = GenNewDateInputs(table);
            string timeInputs = GenNewTimeInputs(table);

            string inputs = GenNewInputs(table);

            string java = string.Format(@"<%@ page language=""java"" contentType=""text/html; charset=UTF-8""
	pageEncoding=""UTF-8""%>
<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">
<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<title>Create New {0}</title>

<link rel=""stylesheet"" href=""css/common.css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-1.8.14.custom.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-timepicker.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/hyjack.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/datepicker.css"" />

<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/common.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.6.1.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.5.1.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.select.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.core.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.widget.min.js""></script>

<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.timepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/ajaxQuickInsert.js""></script>

<link rel=""stylesheet"" href=""css/inputForm.css"" />

<script language=""javascript"" type=""text/javascript"">
	var requiredInputs = [{2}];
	var intInputs = [{3}];
	var doubleInputs = [{4}];
	var dateInputs = [{5}];
	var timeInputs = [{6}];

	function checkInputs() {{
		var valid = true;

		if (checkRequiredInputs(requiredInputs) == false) {{
			valid = false;
		}}
		if (checkIntInputs(intInputs) == false) {{
			valid = false;
		}}
		if (checkDoubleInputs(doubleInputs) == false) {{
			valid = false;
		}}
		if (checkDateInputs(dateInputs) == false) {{
			valid = false;
		}}
		if (checkTimeInputs(timeInputs) == false) {{
			valid = false;
		}}

		// SPECIFIC VALIDATE CODE HERE		

		return valid;
	}}
</script>
</head>
<body>
    <jsp:include page=""menu.jsp""></jsp:include>
	<br />
    <table>
		<tr>
			<td>
	            <form action=""{1}?mode=insert"" method=""post""
                    onSubmit=""return checkInputs()""
                    onreset=""return confirm('Do you really want to reset the form?')"">
		            <table class=""tblInput"" border=""1"">
			            <tr class=""inputHeader"">
				            <th colspan=""3"" class=""inputHeader"">Create New {0}</th>
			            </tr>
            {7}			
			            <tr class=""trInputBottom"">
				            <td class=""tdInputBottom"" colspan=""3"">
					            <table class=""tblSubmit"" align=""center"">
						            <tr class=""trSubmit"">
							            <td class=""tdSubmit""><input type=""submit"" value=""Submit"" />
							            </td>
							            <td class=""tdReset""><input type=""reset"" value=""Reset"" /></td>
						            </tr>
					            </table></td>
			            </tr>
		            </table>
	            </form>
			</td>
		</tr>
	</table>
    <br />	
	<jsp:include page=""{1}List.jsp?embedded=true""></jsp:include>
</body>
</html>", displayName, nameLowerCase, requiredInputs, intInputs, doubleInputs, dateInputs, timeInputs, inputs);

            return java;
        }

        private static string GenNewInputs(PostgreSqlTable table) {

            StringBuilder sb = new StringBuilder();

            foreach (PostgreSqlField field in table.InputFields) {

                if (field.DataType != PostgreSqlDataType.Serial) {

                    #region

                    sb.AppendFormat(@"			<tr class=""trField"">
				<td class=""tdFieldName"">{0}</td>", field.DisplayName);

                    if (field.DataType == PostgreSqlDataType.Bool) {
                        if (field.NotNull) {
                            sb.AppendFormat(@"<td class=""tdFieldInput"">
					<table class=""tblBool"">
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                        } else {
                            sb.AppendFormat(@"<td class=""tdFieldInput"">
					<table class=""tblBool"">
						<tr class=""trBoolSet"">
							<td colspan=""2"" class=""tdBoolSet""><input type=""checkbox""
								id=""ckb_{0}"" name=""ckb_{0}""
								onchange=""toggleControls(['{0}_true', '{0}_false'])"" />Not
								set</td>
						</tr>
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                        }
                    } else if (field.DataType == PostgreSqlDataType.Character) {
                        if (field.MaxLength > 0) {
                            sb.AppendFormat(@"			    <td class=""tdFieldInput"">
					<input class=""inputText"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""{2}"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""abb_name_required"" class=""errMsg"" style=""display: none;"">{0} is required</span>
					<span id=""abb_name_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
				</td>", field.DisplayName, field.Name, field.MaxLength);
                        } else {
                            sb.AppendFormat(@"				    <td class=""tdFieldInput"">
					<textarea id=""{1}"" name=""{1}"" rows=""5"" class=""inputTextarea""></textarea>
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span>
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
				</td>", field.DisplayName, field.Name);
                        }
                    } else if (field.DataType == PostgreSqlDataType.Int) {
                        sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""inputInt"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""10"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span> 
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                </td>", field.DisplayName, field.Name);
                    } else if (field.DataType == PostgreSqlDataType.Numeric) {
                        sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""inputNumeric"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""20"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span> 
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                </td>", field.DisplayName, field.Name);
                    } else if (field.DataType == PostgreSqlDataType.TimeStamp) {
                        sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<table class=""tblDateTime"">
						<tr class=""trDateTime"">
							<td class=""tdDate"">
								<input type=""text"" maxlength=""10"" class=""inputDate"" id=""{1}_date"" name=""{1}_date"" /> 
								<input type=""button"" class=""btnCalendar"" value=""...""
									onclick=""javascript:displayDatePicker('{1}', this, dateFormat);"" />
							</td>
							<td class=""tdTime"">
								<input type=""text"" maxlength=""5"" class=""inputTime"" id=""{1}_time"" name=""{1}_time"" />
							</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg"">
					<div class=""divErr"">
						<span id=""{1}_date_required"" class=""errMsg""
							style=""display: none;"">{0}'s date is required</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_time_required"" class=""errMsg""
							style=""display: none;"">{0}'s time is required</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_date_invalid"" class=""errMsg""
							style=""display: none;"">{0}'s date is invalid</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_time_invalid"" class=""errMsg""
							style=""display: none;"">{0}'s time is invalid</span>
					</div>
				</td>", field.DisplayName, field.Name);
                    }

                    sb.Append(@"</tr>");
                    sb.AppendLine();

                    #endregion

                }
            }

            return sb.ToString();
        }

        private static string GenNewRequiredInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.RequiredFields) {
                if (field.DataType != PostgreSqlDataType.Bool) {
                    if (sb.Length > 0) {
                        sb.Append(",");
                    }
                    if (field.DataType == PostgreSqlDataType.TimeStamp) {
                        sb.AppendFormat(@"['{0}','{0}_date_required', '{0}','{0}_time_required']", field.Name);
                    } else {
                        sb.AppendFormat(@"['{0}','{0}_required']", field.Name);
                    }
                }
            }
            return sb.ToString();
        }

        private static string GenNewIntInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.IntFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenNewDoubleInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DoubleFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenNewDateInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DateTimeFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_date_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenNewTimeInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DateTimeFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_time_invalid']", field.Name);
            }
            return sb.ToString();
        }
        #endregion

        #region INPUT
        public static string GenInputJSP(PostgreSqlTable table) {
            string displayName = table.DisplayName;
            string nameLowerCase = table.JavaNameLowerCase;

            string requiredInputs = GenInputRequiredInputs(table);
            string intInputs = GenInputIntInputs(table);
            string doubleInputs = GenInputDoubleInputs(table);
            string dateInputs = GenInputDateInputs(table);
            string timeInputs = GenInputTimeInputs(table);

            string inputs = GenInputInputs(table);

            string javaName = table.JavaName;

            string java = string.Format(@"<%@ page language=""java"" contentType=""text/html; charset=UTF-8""
	pageEncoding=""UTF-8""%>
<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">

<%@ page import=""model.*, util.*""%>
<%
	{8}Model obj;
	Object temp = request.getAttribute(""{1}"");
	if (temp != null) {{
		try {{
			obj = ({8}Model) temp;
		}} catch (Exception ex) {{
			obj = new {8}Model();
		}}
	}} else {{
		obj = new {8}Model();
	}}
%>

<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<title>Create New {0}</title>

<link rel=""stylesheet"" href=""css/common.css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-1.8.14.custom.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-timepicker.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/hyjack.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/datepicker.css"" />

<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/common.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.6.1.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.5.1.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.select.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.core.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.widget.min.js""></script>

<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.timepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/ajaxQuickInsert.js""></script>

<link rel=""stylesheet"" href=""css/inputForm.css"" />

<script language=""javascript"" type=""text/javascript"">
	var requiredInputs = [{2}];
	var intInputs = [{3}];
	var doubleInputs = [{4}];
	var dateInputs = [{5}];
	var timeInputs = [{6}];

	function checkInputs() {{
		var valid = true;

		if (checkRequiredInputs(requiredInputs) == false) {{
			valid = false;
		}}
		if (checkIntInputs(intInputs) == false) {{
			valid = false;
		}}
		if (checkDoubleInputs(doubleInputs) == false) {{
			valid = false;
		}}
		if (checkDateInputs(dateInputs) == false) {{
			valid = false;
		}}
		if (checkTimeInputs(timeInputs) == false) {{
			valid = false;
		}}

		// SPECIFIC VALIDATE CODE HERE		

		return valid;
	}}
</script>
</head>
<body>
     <jsp:include page=""menu.jsp""></jsp:include>
	<br />
    <table>
		<tr>
			<td>
	            <form action=""{1}?mode=insert"" method=""post""
                    onSubmit=""return checkInputs()""
                    onreset=""return confirm('Do you really want to reset the form?')"">
		            <table class=""tblInput"" border=""1"">
			            <tr class=""inputHeader"">
				            <th colspan=""3"" class=""inputHeader"">Create New {0}</th>
			            </tr>
            {7}			
			            <tr class=""trInputBottom"">
				            <td class=""tdInputBottom"" colspan=""3"">
					            <table class=""tblSubmit"" align=""center"">
						            <tr class=""trSubmit"">
							            <td class=""tdSubmit""><input type=""submit"" value=""Submit"" />
							            </td>
							            <td class=""tdReset""><input type=""reset"" value=""Reset"" /></td>
						            </tr>
					            </table></td>
			            </tr>
		            </table>
	            </form>
            </td>
		</tr>
	</table>
    <br />
	<jsp:include page=""{1}List.jsp?embedded=true""></jsp:include>
</body>
</html>", displayName, nameLowerCase, requiredInputs, intInputs, doubleInputs, dateInputs, timeInputs, inputs,
        javaName, table.Name);

            return java;
        }

        private static string GenInputInputs(PostgreSqlTable table) {

            StringBuilder sb = new StringBuilder();

            foreach (PostgreSqlField field in table.InputFields) {

                if (field.DataType != PostgreSqlDataType.Serial) {

                    #region

                    sb.AppendFormat(@"			<tr class=""trField"">
				<td class=""tdFieldName"">{0}</td>", field.DisplayName);

                    if (field.DataType == PostgreSqlDataType.Serial) {

                    } else if (field.Name == "created" || field.Name == "creator" || field.Name == "updated" || field.Name == "updator"
                        || field.Name == "deletion_time") {

                    } else {
                        if (field.DataType == PostgreSqlDataType.Bool) {
                            if (field.NotNull) {
                                sb.AppendFormat(@"<td class=""tdFieldInput"">
					<table class=""tblBool"">
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                            } else {
                                sb.AppendFormat(@"<td class=""tdFieldInput"">
					<table class=""tblBool"">
						<tr class=""trBoolSet"">
							<td colspan=""2"" class=""tdBoolSet""><input type=""checkbox""
								id=""ckb_{0}"" name=""ckb_{0}""
								onchange=""toggleControls(['{0}_true', '{0}_false'])"" />Not
								set</td>
						</tr>
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                            }
                        } else if (field.DataType == PostgreSqlDataType.Character) {
                            if (field.MaxLength > 0) {
                                sb.AppendFormat(@"			    <td class=""tdFieldInput"">
					<input class=""inputText"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""{2}"" value=""<%=obj.get{3}()%>"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span>
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{3}%></span>
				</td>", field.DisplayName, field.Name, field.MaxLength, field.JavaName);
                            } else {
                                sb.AppendFormat(@"				    <td class=""tdFieldInput"">
					<textarea id=""{1}"" name=""{1}"" rows=""5"" class=""inputTextarea""><%=obj.get{2}()%></textarea>
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span>
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
				</td>", field.DisplayName, field.Name, field.JavaName);
                            }
                        } else if (field.DataType == PostgreSqlDataType.Int) {
                            sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""inputInt"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""10"" value=""<%=obj.get{2}ToString()%>"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span> 
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
                </td>", field.DisplayName, field.Name, field.JavaName);
                        } else if (field.DataType == PostgreSqlDataType.Numeric) {
                            sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""inputNumeric"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""20"" value=""<%=obj.get{2}ToString()%>"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span> 
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
                </td>", field.DisplayName, field.Name, field.JavaName);
                        } else if (field.DataType == PostgreSqlDataType.TimeStamp) {
                            sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<table class=""tblDateTime"">
						<tr class=""trDateTime"">
							<td class=""tdDate"">
								<input type=""text"" maxlength=""10"" class=""inputDate"" id=""{1}_date"" name=""{1}_date"" value=""<%=StringUtil.toDateTimeString(obj.get{2}())%>"" />
								<input type=""button"" class=""btnCalendar"" value=""...""
									onclick=""javascript:displayDatePicker('{1}_date', this, dateFormat);"" />
							</td>
							<td class=""tdTime"">
								<input type=""text"" maxlength=""5"" class=""inputTime"" id=""{1}_time"" name=""{1}_time"" value=""<%=obj.get{2}()%>"" />
							</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg"">
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
					<div class=""divErr"">
						<span id=""{1}_date_required"" class=""errMsg""
							style=""display: none;"">{0}'s date is required</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_time_required"" class=""errMsg""
							style=""display: none;"">{0}'s time is required</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_date_invalid"" class=""errMsg""
							style=""display: none;"">{0}'s date is invalid</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_time_invalid"" class=""errMsg""
							style=""display: none;"">{0}'s time is invalid</span>
					</div>
				</td>", field.DisplayName, field.Name, field.JavaName);
                        }

                        sb.Append(@"</tr>");
                        sb.AppendLine();

                    #endregion
                    }
                }
            }


            return sb.ToString();
        }

        private static string GenInputRequiredInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.RequiredFields) {
                if (field.DataType != PostgreSqlDataType.Bool) {
                    if (sb.Length > 0) {
                        sb.Append(",");
                    }
                    if (field.DataType == PostgreSqlDataType.TimeStamp) {
                        sb.AppendFormat(@"['{0}','{0}_date_required', '{0}','{0}_time_required']", field.Name);
                    } else {
                        sb.AppendFormat(@"['{0}','{0}_required']", field.Name);
                    }
                }
            }
            return sb.ToString();
        }

        private static string GenInputIntInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.IntFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenInputDoubleInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DoubleFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenInputDateInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DateTimeFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_date_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenInputTimeInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DateTimeFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_time_invalid']", field.Name);
            }
            return sb.ToString();
        }
        #endregion

        #region EDIT
        public static string GenEditJSP(PostgreSqlTable table) {
            string displayName = table.DisplayName;
            string nameLowerCase = table.JavaNameLowerCase;

            string requiredInputs = GenEditRequiredInputs(table);
            string intInputs = GenEditIntInputs(table);
            string doubleInputs = GenEditDoubleInputs(table);
            string dateInputs = GenEditDateInputs(table);
            string timeInputs = GenEditTimeInputs(table);

            string inputs = GenEditInputs(table);

            string javaName = table.JavaName;

            string java = string.Format(@"<%@ page language=""java"" contentType=""text/html; charset=UTF-8""
	pageEncoding=""UTF-8""%>
<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">

<%@ page import=""model.*, util.*""%>
<%
	{8}Model obj;
	Object temp = request.getAttribute(""{1}"");
	if (temp != null) {{
		try {{
			obj = ({8}Model) temp;
		}} catch (Exception ex) {{
			obj = new {8}Model();
		}}
	}} else {{
		obj = new {8}Model();
	}}
%>

<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<title>Update {0}</title>

<link rel=""stylesheet"" href=""css/common.css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-1.8.14.custom.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-timepicker.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/hyjack.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/datepicker.css"" />

<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/common.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.6.1.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.5.1.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.select.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.core.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.widget.min.js""></script>

<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.timepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/ajaxQuickInsert.js""></script>

<link rel=""stylesheet"" href=""css/inputForm.css"" />

<script language=""javascript"" type=""text/javascript"">
	var requiredInputs = [{2}];
	var intInputs = [{3}];
	var doubleInputs = [{4}];
	var dateInputs = [{5}];
	var timeInputs = [{6}];

	function checkInputs() {{
		var valid = true;

		if (checkRequiredInputs(requiredInputs) == false) {{
			valid = false;
		}}
		if (checkIntInputs(intInputs) == false) {{
			valid = false;
		}}
		if (checkDoubleInputs(doubleInputs) == false) {{
			valid = false;
		}}
		if (checkDateInputs(dateInputs) == false) {{
			valid = false;
		}}
		if (checkTimeInputs(timeInputs) == false) {{
			valid = false;
		}}

		// SPECIFIC VALIDATE CODE HERE		

		return valid;
	}}
</script>
</head>
<body>
     <jsp:include page=""menu.jsp""></jsp:include>
	<br />
    <table>
		<tr>
			<td>
	            <form action=""{1}?mode=update"" method=""post""
                    onSubmit=""return checkInputs()""
                    onreset=""return confirm('Do you really want to reset the form?')"">
		            <table class=""tblInput"" border=""1"">
			            <tr class=""inputHeader"">
				            <th colspan=""3"" class=""inputHeader"">Create New {0}</th>
			            </tr>
{7}
			            <tr class=""trInputBottom"">
				            <td class=""tdInputBottom"" colspan=""3"">
					            <input type=""submit"" value=""Submit"" />
							</td>
			            </tr>
		            </table>
	            </form>
            </td>
		</tr>
	</table>
    <br />	
	<jsp:include page=""{1}List.jsp?embedded=true""></jsp:include>
</body>
</html>", displayName, nameLowerCase, requiredInputs, intInputs, doubleInputs, dateInputs, timeInputs, inputs,
        javaName, table.Name);

            return java;
        }

        private static string GenEditInputs(PostgreSqlTable table) {

            StringBuilder sb = new StringBuilder();

            foreach (PostgreSqlField field in table.Fields) {

                sb.AppendFormat(@"			<tr class=""trField"">
				<td class=""tdFieldName"">{0}</td>", field.DisplayName);

                if (field.PrimaryKey) {

                    sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""inputInt"" type=""text"" id=""{0}"" name=""{0}"" readonly=""readonly"" value=""<%=obj.get{1}()%>"" />
				</td>
				<td class=""tdErrMsg"">					
                </td>", field.Name, field.JavaName);

                } else if (field.Name == "created" || field.Name == "creator" || field.Name == "updated" || field.Name == "updator"
                    || field.Name == "deletion_time") {

                    sb.AppendFormat(@"				<td class=""tdFieldInput"">
                    <span class=""inputText""><%=obj.get{1}ToString()%></span>					
				</td>
				<td class=""tdErrMsg"">					
                </td>", field.Name, field.JavaName);

                } else {
                    if (field.DataType == PostgreSqlDataType.Bool) {
                        if (field.NotNull) {
                            sb.AppendFormat(@"<td class=""tdFieldInput"">
					<table class=""tblBool"">
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                        } else {
                            sb.AppendFormat(@"<td class=""tdFieldInput"">
					<table class=""tblBool"">
						<tr class=""trBoolSet"">
							<td colspan=""2"" class=""tdBoolSet""><input type=""checkbox""
								id=""ckb_{0}"" name=""ckb_{0}""
								onchange=""toggleControls(['{0}_true', '{0}_false'])"" />Not
								set</td>
						</tr>
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                        }
                    } else if (field.DataType == PostgreSqlDataType.Character) {
                        if (field.MaxLength > 0) {
                            sb.AppendFormat(@"			    <td class=""tdFieldInput"">
					<input class=""inputText"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""{2}"" value=""<%=obj.get{3}()%>"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span>
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{3}%></span>
				</td>", field.DisplayName, field.Name, field.MaxLength, field.JavaName);
                        } else {
                            sb.AppendFormat(@"				    <td class=""tdFieldInput"">
					<textarea id=""{1}"" name=""{1}"" rows=""5"" class=""inputTextarea""><%=obj.get{2}()%></textarea>
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span>
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
				</td>", field.DisplayName, field.Name, field.JavaName);
                        }
                    } else if (field.DataType == PostgreSqlDataType.Int) {
                        sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""inputInt"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""10"" value=""<%=obj.get{2}ToString()%>"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span> 
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
                </td>", field.DisplayName, field.Name, field.JavaName);
                    } else if (field.DataType == PostgreSqlDataType.Numeric) {
                        sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""inputNumeric"" type=""text"" id=""{1}"" name=""{1}"" maxlength=""20"" value=""<%=obj.get{2}ToString()%>"" />
				</td>
				<td class=""tdErrMsg"">
					<span id=""{1}_required"" class=""errMsg"" style=""display: none;"">{0} is required</span> 
					<span id=""{1}_invalid"" class=""errMsg"" style=""display: none;"">{0} is invalid</span>
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
                </td>", field.DisplayName, field.Name, field.JavaName);
                    } else if (field.DataType == PostgreSqlDataType.TimeStamp) {
                        sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<table class=""tblDateTime"">
						<tr class=""trDateTime"">
							<td class=""tdDate"">
								<input type=""text"" maxlength=""10"" class=""inputDate"" id=""{1}_date"" name=""{1}_date"" value=""<%=StringUtil.toDateTimeString(obj.get{2}())%>"" /> 
								<input type=""button"" class=""btnCalendar"" value=""...""
									onclick=""javascript:displayDatePicker('{1}_date', this, dateFormat);"" />
							</td>
							<td class=""tdTime"">
								<input type=""text"" maxlength=""5"" class=""inputTime"" id=""{1}_time"" name=""{1}_time"" value=""<%=obj.get{2}()%>"" />
							</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg"">
                    <span id=""{1}_error"" class=""errMsg""><%=obj.errMsgs.{2}%></span>
					<div class=""divErr"">
						<span id=""{1}_date_required"" class=""errMsg""
							style=""display: none;"">{0}'s date is required</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_time_required"" class=""errMsg""
							style=""display: none;"">{0}'s time is required</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_date_invalid"" class=""errMsg""
							style=""display: none;"">{0}'s date is invalid</span>
					</div>
					<div class=""divErr"">
						<span id=""{1}_time_invalid"" class=""errMsg""
							style=""display: none;"">{0}'s time is invalid</span>
					</div>
				</td>", field.DisplayName, field.Name, field.JavaName);
                    }

                    sb.Append(@"</tr>");
                    sb.AppendLine();
                }
            }

            return sb.ToString();
        }

        private static string GenEditRequiredInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.RequiredFields) {
                if (field.DataType != PostgreSqlDataType.Bool) {
                    if (sb.Length > 0) {
                        sb.Append(",");
                    }
                    if (field.DataType == PostgreSqlDataType.TimeStamp) {
                        sb.AppendFormat(@"['{0}','{0}_date_required', '{0}','{0}_time_required']", field.Name);
                    } else {
                        sb.AppendFormat(@"['{0}','{0}_required']", field.Name);
                    }
                }
            }
            return sb.ToString();
        }

        private static string GenEditIntInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.IntFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenEditDoubleInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DoubleFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenEditDateInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DateTimeFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_date_invalid']", field.Name);
            }
            return sb.ToString();
        }

        private static string GenEditTimeInputs(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();
            foreach (PostgreSqlField field in table.DateTimeFields) {
                if (sb.Length > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat(@"['{0}','{0}_time_invalid']", field.Name);
            }
            return sb.ToString();
        }
        #endregion

        #region VIEW
        public static string GenViewJSP(PostgreSqlTable table) {

            string displayName = table.DisplayName;
            string nameLowerCase = table.JavaNameLowerCase;
            string inputs = GenViewInputs(table);
            string javaName = table.JavaName;

            string java = string.Format(@"<%@ page language=""java"" contentType=""text/html; charset=UTF-8""
	pageEncoding=""UTF-8""%>
<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">

<%@ page import=""model.*, util.*""%>
<%
	{0}Model obj;
	Object temp = request.getAttribute(""{2}"");
	if (temp != null) {{
		try {{
			obj = ({0}Model) temp;
		}} catch (Exception ex) {{
			obj = new {0}Model();
		}}
	}} else {{
		obj = new {0}Model();
	}}
%>

<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<title>Update {2}</title>

<link rel=""stylesheet"" href=""css/common.css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-1.8.14.custom.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-timepicker.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/hyjack.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/datepicker.css"" />

<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/common.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.6.1.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.5.1.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.select.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.core.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.widget.min.js""></script>

<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.timepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/ajaxQuickInsert.js""></script>

<link rel=""stylesheet"" href=""css/inputForm.css"" />

</head>
<body>
     <jsp:include page=""menu.jsp""></jsp:include>
    <br />    
	<table class=""tblInput"" border=""1"">
		<tr class=""inputHeader"">
			<th colspan=""3"" class=""inputHeader"">{3} Information</th>
		</tr>
{4}			
<!--
		<tr class=""trInputBottom"">
			<td class=""trInputBottom"" colspan=""3"">
				<table class=""tblSubmit"" align=""center"">
					<tr class=""trSubmit"">
						<td class=""tdSubmit"">
							<input type=""submit"" value=""Edit"" />
						</td>
					</tr>
				</table></td>
		</tr>
-->
	</table>
    <br />	
	<jsp:include page=""{2}List.jsp?embedded=true""></jsp:include>
</body>
</html>", javaName, table.Name, nameLowerCase, displayName, inputs);

            return java;
        }

        private static string GenViewInputs(PostgreSqlTable table) {

            StringBuilder sb = new StringBuilder();

            foreach (PostgreSqlField field in table.Fields) {

                sb.AppendFormat(@"			<tr class=""trField"">
				<td class=""tdFieldName"">{0}</td>", field.DisplayName);

                if (field.PrimaryKey) {
                    sb.AppendFormat(@"				<td class=""tdFieldInput"">
					<input class=""lblInt"" type=""text"" id=""{0}"" name=""{0}"" readonly=""readonly"" value=""<%=obj.get{1}ToString()%>"" />
				</td>
				<td class=""tdErrMsg"">					
                </td>", field.Name, field.JavaName);

                } else {
                    if (field.DataType == PostgreSqlDataType.Bool) {
                        if (field.NotNull) {
                            sb.AppendFormat(@"<td class=""tdFieldValue"">
					<table class=""tblBool"">
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                        } else {
                            sb.AppendFormat(@"<td class=""tdFieldValue"">
					<table class=""tblBool"">
						<tr class=""trBoolSet"">
							<td colspan=""2"" class=""tdBoolSet""><input type=""checkbox""
								id=""ckb_{0}"" name=""ckb_{0}""
								onchange=""toggleControls(['{0}_true', '{0}_false'])"" />Not
								set</td>
						</tr>
						<tr class=""trBool"">
							<td class=""tdBool""><input type=""radio""
								id=""{0}_true"" name=""{0}"" value=""1""
								checked=""checked"" />Yes</td>
							<td class=""tdBool""><input type=""radio""
								id=""{0}_false"" name=""{0}"" value=""0"" />No</td>
						</tr>
					</table></td>
				<td class=""tdErrMsg""></td>", field.Name);
                        }
                    } else if (field.DataType == PostgreSqlDataType.Character) {
                        if (field.MaxLength > 0) {
                            sb.AppendFormat(@"			    <td class=""tdFieldValue"">
                    <span class=""lblText""><%=obj.get{0}ToString()%></span>
				</td>", field.JavaName);
                        } else {
                            sb.AppendFormat(@"				    <td class=""tdFieldInput"">

					<textarea id=""{1}"" name=""{1}"" rows=""5"" class=""lblTextarea"" readonly=""readonly""><%=obj.get{2}ToString()%></textarea>
				</td>", field.DisplayName, field.Name, field.JavaName);
                        }
                    } else if (field.DataType == PostgreSqlDataType.Int) {
                        sb.AppendFormat(@"				<td class=""tdFieldValue"">
                    <span class=""lblInt""><%=obj.get{0}ToString()%></span>
				</td>", field.JavaName);
                    } else if (field.DataType == PostgreSqlDataType.Numeric) {
                        sb.AppendFormat(@"				<td class=""tdFieldValue"">
                    <span class=""lblNumeric""><%=obj.get{0}ToString()%></span>
				</td>", field.JavaName);
                    } else if (field.DataType == PostgreSqlDataType.TimeStamp) {
                        sb.AppendFormat(@"				<td class=""tdFieldValue"">
                     <span class=""lblDateTime""><%=StringUtil.toDateTimeString(obj.get{0}())%></span>   
					</td>", field.JavaName);
                    }

                    sb.Append(@"</tr>");
                    sb.AppendLine();
                }
            }

            return sb.ToString();
        }

        #endregion

        #region LIST
        public static string GenListJSP(PostgreSqlTable table) {

            string javaName = table.JavaName;
            string javaNameLowerCase = table.JavaNameLowerCase;
            string displayName = table.DisplayName;

            string headers = GenListHeaders(table);
            string items = GenListItems(table);

            string ids = GenListIds(table);

            string java = string.Format(@"<%@ page language=""java"" contentType=""text/html; charset=UTF-8""
	pageEncoding=""UTF-8""%>
<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">

<%@page import=""util.*""%>
<%@page import=""dal.*""%>
<%@ page import=""model.*,java.util.*""%>
<%
	GenericDal<{0}Model, {0}ModelExt> dal = new GenericDal<{0}Model, {0}ModelExt>(
			new {0}Model());
	{0}ModelExt queryCount = new {0}ModelExt();
	int count = dal.count(queryCount);

	int pageIndex = 1;
	if (request.getParameter(""pageIndex"") != null
			&& !request.getParameter(""pageIndex"").isEmpty()) {{
		try {{
			pageIndex = Integer.parseInt(request
					.getParameter(""pageIndex""));
		}} catch (Exception e) {{
		}}
	}}

	int pageSize = 20;
	if (request.getParameter(""pageSize"") != null
			&& !request.getParameter(""pageSize"").isEmpty()) {{
		try {{
			pageSize = Integer.parseInt(request
					.getParameter(""pageSize""));
		}} catch (Exception e) {{
		}}
	}}

	int noOfPage = count / pageSize;
	if (count % pageSize > 0) {{
		noOfPage++;
	}}

	{0}ModelExt query = new {0}ModelExt();
	query.setLimit(pageSize);
	query.setOffset((pageIndex - 1) * pageSize);
	List<{0}Model> list = dal.selectList(query);

	boolean embedded = false;
	if (request.getParameter(""embedded"") != null
			&& !request.getParameter(""embedded"").isEmpty()) {{

		if (request.getParameter(""embedded"").equalsIgnoreCase(""true"")
				|| request.getParameter(""embedded"").equalsIgnoreCase(
						""1"")) {{
			embedded = true;
		}}
	}}
%>

<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<title>{2} List</title>

<link rel=""stylesheet"" href=""css/common.css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-1.8.14.custom.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/jquery-ui-timepicker.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/hyjack.css"" type=""text/css"" />
<link rel=""stylesheet"" href=""css/datepicker.css"" />

<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/common.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.6.1.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery-1.5.1.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.select.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.core.min.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.widget.min.js""></script>

<script language=""javascript"" type=""text/javascript"" src=""js/jquery.ui.timepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/datepicker.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/jquery.hyjack.js""></script>
<script language=""javascript"" type=""text/javascript"" src=""js/ajaxQuickInsert.js""></script>

<link rel=""stylesheet"" href=""css/listForm.css"" />

</head>
<body>
	<%
		if (!embedded)
		{{
	%>
	<jsp:include page=""menu.jsp""></jsp:include>
    <br />
	<%
		}}
	%>
	<table class=""tblPaging"">
		<tr class=""trPaging"">
			<td class=""tdPaging"">				
                <%
					for (int i = 1; i <= noOfPage; i++) {{
						if (i == pageIndex) {{
				%> 
					<span class=""lblPaging""><%=i%></span> 
				<%
				 		}} else {{
				 %> 
				 	<a class=""hrefPaging"" href=""{1}List.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>""><%=i%></a>
				<%
						}}
					}}
				%>
			</td>
		</tr>
	</table>
    <br />
	<table class=""tblList"">
		<tr class=""tblListHeader"">			
			<th class=""tdListHeader""></th>
            <th class=""tdListHeader""></th>
{3}
            <th class=""tdListHeader""></th>
		</tr>

		<%
			int size = list.size();
			for (int i = 0; i < size; i++) {{
				{0}Model obj = list.get(i);
		%>

		<tr class=""trListItem"">
			<td class=""tdListInt""><%=i + 1%></td>
			<td class=""tdListHref"">
				<a href=""{1}?mode=edit{5}"">Edit</a>
			</td>
{4}
            <td class=""tdListHref"">
				<a href=""{1}?mode=delete{5}"">Delete</a>
			</td>
		</tr>
		<%
			}}
		%>
	</table>
    <br />
    <table class=""tblPaging"">
		<tr class=""trPaging"">
			<td class=""tdPaging"">				
                <%
					for (int i = 1; i <= noOfPage; i++) {{
						if (i == pageIndex) {{
				%> 
					<span class=""lblPaging""><%=i%></span> 
				<%
				 		}} else {{
				 %> 
				 	<a class=""hrefPaging"" href=""{1}List.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>""><%=i%></a>
				<%
						}}
					}}
				%>
			</td>
		</tr>
	</table>
</body>
</html>", javaName, javaNameLowerCase, displayName, headers, items, ids);

            return java;
        }

        private static string GenListIds(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < table.PrimaryKeys.Count; i++) {
                PostgreSqlField field = table.PrimaryKeys[i];
                //if (i > 0) {
                sb.AppendFormat(@"&");
                //}
                sb.AppendFormat(@"{0}=<%=obj.get{1}()%>", field.Name, field.JavaName);
                sb.AppendLine();
            }

            return sb.ToString();
        }

        private static string GenListHeaders(PostgreSqlTable table) {
            StringBuilder sb = new StringBuilder();

            foreach (PostgreSqlField field in table.Fields) {
                sb.AppendFormat(@"<th class=""thListHeader"">{0}</th>", field.DisplayName);
                sb.AppendLine();
            }

            return sb.ToString();
        }

        private static string GenListItems(PostgreSqlTable table) {

            StringBuilder sb = new StringBuilder();

            foreach (PostgreSqlField field in table.Fields) {
                string cssClass = "tdList";

                if (field.DataType == PostgreSqlDataType.Bool) {
                    cssClass = "tdListBool";
                    sb.AppendFormat(@"<td class=""{0}""><%=HtmlUtil.genHtml(obj.get{1}())%></td>", cssClass, field.JavaName);
                } else if (field.DataType == PostgreSqlDataType.Character) {
                    if (field.MaxLength > 0) {
                        cssClass = "tdListText";
                    } else {
                        cssClass = "tdListTextarea";
                    }
                    sb.AppendFormat(@"<td class=""{0}""><%=obj.get{1}ToString()%></td>", cssClass, field.JavaName);
                } else if (field.DataType == PostgreSqlDataType.Int) {
                    cssClass = "tdListInt";
                    sb.AppendFormat(@"<td class=""{0}""><%=obj.get{1}ToString()%></td>", cssClass, field.JavaName);
                } else if (field.DataType == PostgreSqlDataType.Numeric) {
                    cssClass = "tdListNumeric";
                    sb.AppendFormat(@"<td class=""{0}""><%=obj.get{1}ToString()%></td>", cssClass, field.JavaName);
                } else if (field.DataType == PostgreSqlDataType.TimeStamp) {
                    cssClass = "tdListDateTime";
                    sb.AppendFormat(@"<td class=""{0}""><%=StringUtil.toDateTimeString(obj.get{1}())%></td>", cssClass, field.JavaName);
                }


                sb.AppendLine();
            }


            return sb.ToString();
        }

        #endregion

    }
}
